<?php
/**
*   转盘
*/
namespace app\api\model;
use think\Model;
use think\Db;

class Turntable extends Model
{
    /**
    * 抽奖
    */
    public function _draw($mid){
        $model = new Member;
        $udid = $model->_getUdidByID($mid);
        
        $tmp = array();
        $tt_data = $this->_getAwards();
        foreach($tt_data as $k=>$v){
            $tmp[] = array('id'=>$v['id'],'prize'=>$v['title'], 'v'=>(int)$v['probability']);
        }
        $prize_arr = $tmp;
        //var_dump($prize_arr);die;
        $res = $this->_getRandResulte($prize_arr);
        $award = $this->where(array('title'=>$res))->find();
        if(empty($award)){
            $this->error = '很遗憾没有中奖';
            return false;
        }
        
        $tmp = $this->_getAwards();
        $award_id = $award['id'];
        
        foreach($tmp as $k=>$v){
            if($v['id'] == $award['id']){
                $award_id = $k;
                break;
            }
        }
        
        
        $gold = 0;
        
        $result = [
            'info'=>'恭喜获奖：'.$res,
            'id'=>$award_id
        ];
        $ip = ip2long(get_ip());
        //var_dump($res);die;
        if($res == '谢谢惠顾' || $res == '再接再厉'){
            $time = time();
            $this->startTrans();
            $sql = "update sy_member_info set onday_turntable_num = onday_turntable_num - 1 where mid = $mid and onday_turntable_num >= 1";
            $ret = Db::execute($sql);
            if(!$ret){
                $this->error = '抽奖次数不足';
                $this->rollback();
                return false;
            }
            
            $sql = "insert sy_turntable_record (mid, remark, ip,created) select $mid, '$res', $ip, $time from sy_member_info where mid =  $mid and onday_turntable_num > 0";
            $ret = Db::execute($sql);
            if(!$ret){
                $this->error = '抽奖次数不足';
                $this->rollback();
                return false;
            }
            
            $this->commit();
            $result = [
                'info'=> $res,
                'id'=>$award_id
            ];
            return $result;
        }else{
            $time = time();
            $gold = $award['gold'];
            
            $this->startTrans();
            
            
            
            $sql = "insert sy_turntable_record(mid, remark, ip, created) select $mid, '$res', $ip, $time from sy_member_info where mid = $mid and onday_turntable_num > 0";
            //echo $sql;
            $ret = Db::execute($sql);
            if(!$ret){
                $this->error = '抽奖次数不足';
                $this->rollback();
                return false;
            }
            $type_id = 3;
            $serial_number = serialNumber();
            $remark = '大转盘：抽奖获得'.$gold.'金币';
            
            $data = [
                'mid' => $mid,
                'udid' => $udid,
                'type_id' => $type_id,
                'serial_number' => $serial_number,
                'gold' => $gold,
                'remark' => $remark,
                'created' => time(),
            ];
            
            $sql = "insert sy_gold_record (mid, udid, type_id, serial_number, gold, remark, ip, created) select $mid, '$udid', $type_id, '$serial_number', $gold, '$remark', $ip, $time from sy_member_info where mid = $mid and onday_turntable_num > 0";
            $ret = Db::execute($sql);
            if(!$ret){
                $this->error = '抽奖次数不足';
                $this->rollback();
                return false;
            }
            
            $sql = "update sy_member set gold = gold + $gold where id = $mid and status = 1 and (select onday_turntable_num as num from sy_member_info  where mid = $mid ) > 0";
            $ret = Db::execute($sql);
            if(!$ret){
                $this->error = '抽奖失败';
                $this->rollback();
                return false;
            }
            
            $sql = "update sy_member_info set onday_turntable_num = onday_turntable_num - 1, total_gold = total_gold + $gold where mid = $mid and onday_turntable_num >= 1";
            $ret = Db::execute($sql);
            if(!$ret){
                $this->error = '抽奖次数不足';
                $this->rollback();
                return false;
            }
            
            $this->commit();
            
            return $result;
        }
    }
    
    //获取中奖结果
    public function _getRandResulte($prize_arr){

        foreach ($prize_arr as $key => $val) { 
            $arr[$val['id']] = $val['v']; //将$prize_arr放入数组下标为$prize_arr的id元素，值为v元素的数组中
        } 
        
        
        $rid = getRand($arr); //根据概率获取奖项id 
        $aid = 0;
        foreach($prize_arr as $k=>$v){
            if($rid == $v['id']){
                $aid = $k;
                break;
            }
        }
        $res['yes'] = $prize_arr[$aid]['prize']; //获取中奖项 

        unset($prize_arr[$aid]); //将中奖项从数组中剔除，剩下未中奖项 
        shuffle($prize_arr); //打乱数组顺序 
        for($i=0;$i<count($prize_arr);$i++){ 
            $pr[] = $prize_arr[$i]['prize']; 
        } 
        $res['no'] = $pr;
        return $res['yes'];
    }
    
    public function _getAwards(){
        return $this->field('id, title, probability, gold')->where([['status', '=', 1]])->order('sort desc')->select();
    }

}